package com.duowan.realtime.hash;

import java.util.Random;

import com.duowan.realtime.server.util.HashUtils;

public class RandomNumberHashFunction extends AbstractCustomHashFunction{

	@Override
	public int[] hash(byte[] value, int m, int k) {
		setK(k);
		setM(m);
		return hashRNG(value);
	}
	
	/**
	 * Java随机数发生器
	 * number_i+1 = (a * number_i + c) mod m</code><br/>
	 * @param value the value to be hashed
	 * @return array with <i>k</i> integer hash positions in the range <i>[0,m)</i>
	 */
	protected int[] hashRNG(byte[] value) {
		int[] positions = new int[k];
		Random r = new Random(HashUtils.hashBytes(value));
		for (int i = 0; i < k; i++) {
			positions[i] = r.nextInt(m);
		}
		return positions;
	}

}
